//
//  ProductCategoriesSelectVC.m
//  AFNetworking
//
//  Created by admin on 2020/4/28.
//

#import "ProductCategoriesSelectVC.h"
#import "ProductCategoriesSelectCell.h"
@interface ProductCategoriesSelectVC ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UIButton *navEditBtn;
@property(strong,nonatomic) UITableView *firstTableView;
@property(strong,nonatomic) UITableView *secondTableView;
@property(strong,nonatomic) UITableView *thirdTableView;
@property (nonatomic, strong) NSMutableDictionary *selectedResult;
@property (nonatomic, strong) NSMutableDictionary *result;
@property (nonatomic, strong) NSMutableArray * datalist;
@end

@implementation ProductCategoriesSelectVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self loadData:NULL key:@"0"];
}

-(void)configUI{
    self.title = @"选择商品类目";
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
     initWithCustomView:self.navEditBtn];
     [self.navigationItem setRightBarButtonItem:buttonItem];

    [self.view addSubview:self.firstTableView];
    [self.view addSubview:self.secondTableView];
    [self.view addSubview:self.thirdTableView];
    
    
}

-(UIButton *)navEditBtn{
    if (!_navEditBtn) {
        _navEditBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
        [_navEditBtn setFrame:CGRectMake(0, 0, 30, 40)];
        [_navEditBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_navEditBtn setTitle:@"确认" forState:UIControlStateSelected];
        [_navEditBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [_navEditBtn addTarget:self action:@selector(rightBarButtonItemClick:) forControlEvents:UIControlEventTouchUpInside];
         [self.navEditBtn setHidden:YES];
    }
    return _navEditBtn;

}

-(void)rightBarButtonItemClick:(UIButton*)sender{
    _data.categotis = [_selectedResult objectForKey:@"2"];
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSMutableDictionary *)result{
    if (!_result) {
        _result = [NSMutableDictionary new];
    }
    return _result;
}
-(NSMutableDictionary *)selectedResult{
    if (!_selectedResult) {
           _selectedResult = [NSMutableDictionary new];
       }
       return _selectedResult;
}
-(NSMutableArray *)datalist{
    if (!_datalist) {
        _datalist = [@[@"美食 ",@"甜品快快快...",@"甜品",@"火锅",@"快餐"] mutableCopy];
    }
    return _datalist;
}
-(UITableView *)firstTableView{
    
    if (!_firstTableView) {
        _firstTableView = [UITableView new];
        _firstTableView.delegate = self;
        _firstTableView.dataSource = self;
        _firstTableView.backgroundColor = UIColor.clearColor;
        [_firstTableView setTableFooterView:[UIView new]];
        [_firstTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//        [_firstTableView ug_shadowColor:[UIColor ug_R:0 G:0 B:0 A:1] width:14];
    }
    return _firstTableView;
}
-(UITableView *)secondTableView{
    
    if (!_secondTableView) {
        _secondTableView = [UITableView new];
        _secondTableView.delegate = self;
        _secondTableView.dataSource = self;
        _secondTableView.hidden = YES;
        _secondTableView.backgroundColor = UIColor.whiteColor;
        [_secondTableView setTableFooterView:[UIView new]];
         [_secondTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_secondTableView ug_shadowColor:[UIColor ug_R:0 G:0 B:0 A:1] width:1];
    }
    return _secondTableView;
}
-(UITableView *)thirdTableView{
    
    if (!_thirdTableView) {
        _thirdTableView = [UITableView new];
        _thirdTableView.delegate = self;
        _thirdTableView.dataSource = self;
        _thirdTableView.hidden = YES;
        [_thirdTableView setTableFooterView:[UIView new]];
         [_thirdTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _thirdTableView.backgroundColor = UIColor.whiteColor;
        [_thirdTableView ug_shadowColor:[UIColor ug_R:0 G:0 B:0 A:1] width:1];
    }
    return _thirdTableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _firstTableView) {
        NSArray *dataarr = [_result objectForKey:@"0"];
        return dataarr.count;
    }else if(tableView == _secondTableView){
        NSArray *dataarr = [_result objectForKey:@"1"];
        return dataarr.count;
    }else{
        NSArray *dataarr = [_result objectForKey:@"2"];
        return dataarr.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    ProductCategoriesSelectCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[ProductCategoriesSelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.remarkView.hidden = YES;
    if (tableView == _firstTableView) {
        NSArray *dataarr = [_result objectForKey:@"0"];
        NSString *celldata = [dataarr objectAtIndex:indexPath.row];
        NSDictionary *dic = [_selectedResult objectForKey:@"0"];
        if ([celldata isEqual:dic]) {
            cell.remarkView.hidden = NO;
            cell.backgroundColor = [UIColor whiteColor];
            
        }else{
            cell.backgroundColor = [UIColor ug_hexString:@"#F3F5F9"];
        }
        cell.textLabel.text = celldata;
        
    }else if(tableView == _secondTableView){
        NSArray *dataarr = [_result objectForKey:@"1"];
        NSString *celldata = [dataarr objectAtIndex:indexPath.row];
        NSDictionary *dic = [_selectedResult objectForKey:@"1"];
        if ([celldata isEqual:dic]) {
        
            cell.textLabel.textColor = [UIColor ug_hexString:@"#007AFF"];
        }else{
            cell.textLabel.textColor = COLOR_33;
        }
        cell.textLabel.text = [dataarr objectAtIndex:indexPath.row];
    }else{
        NSArray *dataarr = [_result objectForKey:@"2"];
        NSString *celldata = [dataarr objectAtIndex:indexPath.row];
        NSDictionary *dic = [_selectedResult objectForKey:@"2"];
        if ([celldata isEqual:dic]) {
            cell.textLabel.textColor = [UIColor ug_hexString:@"#007AFF"];
        }else{
            cell.textLabel.textColor = COLOR_33;
        }
        cell.textLabel.text = [dataarr objectAtIndex:indexPath.row];
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [self.navEditBtn setHidden:YES];
    if (tableView == _firstTableView) {
        id data = [_datalist objectAtIndex:indexPath.row];
        [self.selectedResult setObject:data forKey:@"0"];
        [self.selectedResult removeObjectForKey:@"1"];
        [self.selectedResult removeObjectForKey:@"2"];
        _secondTableView.hidden = YES;
        _thirdTableView.hidden = YES;
        [self loadData:data key:@"1"];
    }else if(tableView == _secondTableView){
        id data = [_datalist objectAtIndex:indexPath.row];
        [self.selectedResult setObject:data forKey:@"1"];
        [self.selectedResult removeObjectForKey:@"2"];
        _thirdTableView.hidden = YES;
        [self loadData:data key:@"2"];
       
    }else{
        id data = [_datalist objectAtIndex:indexPath.row];
        [self.selectedResult setObject:data forKey:@"2"];
          [self.navEditBtn setHidden:NO];
    }
    [tableView reloadData];
}

-(void)loadData:(NSString*)parsentCode key:(NSString*)key{
    [self.view ug_starloading];
    UG_WEAKSELF
    [self bk_performBlock:^(id obj) {
        switch ([key intValue]) {
            case 0:{
                [weakSelf.result setObject:weakSelf.datalist forKey:key];
                [weakSelf.firstTableView reloadData];
            }
                break;
            case 1:{
                [weakSelf.result setObject:weakSelf.datalist forKey:key];
                weakSelf.secondTableView.hidden = NO;
                [weakSelf.secondTableView reloadData];
            }
                break;
                
            default:{
                [weakSelf.result setObject:weakSelf.datalist forKey:key];
                weakSelf.thirdTableView.hidden = NO;
                [weakSelf.thirdTableView reloadData];
            }
                break;
        }
         [self.view ug_stoploading];
    } afterDelay:0.75];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_firstTableView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(0.0);
         make.top.equalTo(self.view.mas_top).offset(0.0);
         make.width.mas_equalTo(KWidth);
         make.bottom.mas_equalTo(0);
     }];
     
     [_secondTableView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(KWidth/4);
         make.top.equalTo(self.view.mas_top).offset(0.0);
         make.width.mas_equalTo(KWidth);
         make.bottom.mas_equalTo(self.firstTableView);
     }];
     
     [_thirdTableView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(5*KWidth/8);
         make.top.equalTo(self.view.mas_top).offset(0.0);
         make.width.mas_equalTo(KWidth);
         make.bottom.mas_equalTo(self.firstTableView);
     }];
}


@end
